/**
 * Created by zeopean on 2015/9/19.
 */

$(function(){
    /** 初始化ajax */
    jQuery.ajaxSetup({
        case:false,
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });

   /** 项目详细信息的添加 */
   $('.save_detail').bind('click' , function(){
       var id = $('#project_id').val();
       var url = EditProject + id ;

       $.ajax({
           type:"post",
           url:url,
           dataType:"json",
           data:$('#form_detail').serialize(),
           success:function(obj){
               if(obj.level === "success"){
                   noty({text:obj.message,type:obj.level,timeout:500});
                   $('#project_id').val(obj.data.id);
               }
           },
           error:function(obj){
               var datajson = obj.responseJSON;
               noty({text:datajson.message,type:datajson.level,timeout:1000});
           }
       });
   });


   /** 编辑行业信息 */
   $(document).on('click','.field',function(){
       var checkbox = $(this);
       var pro_id   = $('#project_id').val();
       var field_id = $(this).val();

       if(!checkbox.is(':checked')){
           $.ajax({
               type:"post",
               url:delProjectField,
               dataType:"json",
               data:{
                   'pro_id'     :   pro_id,
                   'field_id'   :   field_id
               },
               success:function(obj){

               },
               error:function(obj){
                   var datajson = obj.responseJSON;
                   noty({text:datajson.message,type:datajson.level,timeout:1000});
               }
           });
       }
   });


   /** end */

   /** 项目介绍 */
    $('.save_intro').bind('click' , function(){
        var id = $('#project_id').val();
        if(id == 0){
            noty({text:'您的项目名不能为空',type:'error',timeout:1000});
            return false;
        }
        var url = EditProject + id ;

        /** 项目总览 */
        var temp    = $('.del-btn');
        var ids     = '';
        temp.each(function(i , item){
            i = i-1;
            ids += temp.eq(i).attr('data')+',';
        });

        $.ajax({
            type:"post",
            url:url,
            dataType:"json",
            data:$('#form_intro').serialize(),
            success:function(obj){
                if(obj.level === "success"){
                    noty({text:obj.message,type:obj.level,timeout:500});
                }
            },
            error:function(obj){
                var datajson = obj.responseJSON;
                noty({text:datajson.message,type:datajson.level,timeout:1000});
            }
        });
    });

    /** 删除项目总览图片 */
    $(document).on('click' ,'.del-btn' , function(){
        var pro_id = $('#project_id').val();
        var img_id = $(this).attr('data');
        var own    = $(this);

        $.ajax({
            type:"post",
            url:delOverviewImage,
            dataType:"json",
            data:{
                'pro_id':pro_id,
                'img_id':img_id
            },
            success:function(obj){
                if(obj.level === "success"){
                    own.parent('span').hide();
                }else{
                    noty({text:obj.message,type:obj.level,timeout:100});
                }
            },
            error:function(obj){
                var datajson = obj.responseJSON;
                noty({text:datajson.message,type:datajson.level,timeout:1000});
            }
        });

    });



    /** end */

    /** 商业计划书 */
    $('.save_planbook').bind('click' , function(){
        var id = $('#project_id').val();
        if(id == 0){
            noty({text:'您的项目名不能为空',type:'error',timeout:1000});
            return false;
        }
        var url = EditProject + id ;

        $.ajax({
            type:"post",
            url:url,
            dataType:"json",
            data:$('#form_planbook').serialize(),
            success:function(obj){
                if(obj.level === "success"){
                    noty({text:obj.message,type:obj.level,timeout:500});
                }
            },
            error:function(obj){
                var datajson = obj.responseJSON;
                noty({text:datajson.message,type:datajson.level,timeout:1000});
            }
        });
    });
    /** end */


    /** 项目需求融资 */
    $('.save_need').bind('click' , function(){
        var id = $('#project_id').val();
        if(id == 0){
            noty({text:'您的项目名不能为空',type:'error',timeout:1000});
            return false;
        }
        $('#form_need input[name=project_id]').val(id);
        var url = EditProject + id ;

        $.ajax({
            type:"post",
            url:url,
            dataType:"json",
            data:$('#form_need').serialize(),
            success:function(obj){
                if(obj.level === "success"){
                    noty({text:obj.message,type:obj.level,timeout:500});
                }
            },
            error:function(obj){
                var datajson = obj.responseJSON;
                noty({text:datajson.message,type:datajson.level,timeout:1000});
            }
        });
    });
    /** end */

    /** 项目指数 */
    $('.save_index').bind('click' , function(){
        var id = $('#project_id').val();
        if(id == 0){
            noty({text:'您的项目名不能为空',type:'error',timeout:1000});
            return false;
        }
        $('#form_index input[name=project_id]').val(id);
        var url = EditProject + id ;

        $.ajax({
            type:"post",
            url:url,
            dataType:"json",
            data:$('#form_index').serialize(),
            success:function(obj){
                if(obj.level === "success"){
                    noty({text:obj.message,type:obj.level,timeout:500});
                }
            },
            error:function(obj){
                var datajson = obj.responseJSON;
                noty({text:datajson.message,type:datajson.level,timeout:1000});
            }
        });
    });
    /** end */

    /** 通过项目标识 删除项目 */
    $('.del').bind('click' , function(){
        var proId   = $(this).attr('data');
        var tmp     = $(this);
        $.ajax({
            type:"get",
            url:DeleteProjectById,
            dataType:"json",
            data:{
                'proId':proId
            },
            success:function(obj){
                if(obj.level === "success"){
                    noty({text:obj.message,type:obj.level,timeout:500});
                    tmp.parents('tr').hide();
                }
            },
            error:function(obj){
                var datajson = obj.responseJSON;
                noty({text:datajson.message,type:datajson.level,timeout:1000});
            }
        });
    });

    /** end */

    /** 链接focus */
    $('input[name=project_url]').bind('focus' , function(){
        var url = $(this).val();
        if(url == ''){
            $(this).val('http://');
        }
    });
    /** end */




});
